<?php

namespace App\Http\Controllers;

use App\Models\Doctor;
use App\Util\Tool;
use Encore\Admin\Form\Tools;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class ResetPassController extends BaseController
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function reset(Request $request)
    {
        //判断密码是否为空
        if (empty($request->password)) {
            return $this->ResultJson(0, '原密码不能为空!', []);
        }
        //判断新密码是否为空
        if (empty($request->new_password)) {
            return $this->ResultJson(0, '新密码不能为空!', []);
        }
        //判断确认密码是否为空
        if (empty($request->password_confirmation)) {
            return $this->ResultJson(0, '确认密码不能为空!', []);
        }
        //密码对比是否正确
        if ($request->password_confirmation != $request->new_password) {
            return $this->ResultJson(0, '两次密码不一致!', []);
        }
        //检查是否登录
        if (!Tool::GetLoginToken($request->api_token)) {
            return $this->ResultJson(0, 'api_token不能为空!', []);
        }
        $user = Tool::ApiTokenGetDoctorUser($request->api_token);
        //检查是否登录
        if (empty($user)) {
            return $this->ResultJson(0, '请先登录后再操作!', []);
        }
        //校验原密码是否正确
        if (Hash::check($request->password, $user->password) == false) {
            return $this->ResultJson(0, '原密码错误', []);
        }
        $user->password = bcrypt($request->new_password);
        $user->save();
        //更新token
        Tool::SetDoctorAndUpToken($request->api_token, $user->account);
        return $this->ResultJson(1, '修改成功!', []);
    }
}
